#!/bin/sh
#
# Merge local ports with FreeBSD ports
#
# Files are automatically copied, files that need to be merged by hand are
# listed on stdout.

PORTSDIR=${PORTSDIR:=/var/cache/portshaker/freebsd}

# Unexpand any expanded VCS tag (e.g. $Id$)
unsubstitute_keywords()
{
	sed -I '' -E -e 's#\$(FreeBSD|Id|MCom):[^\$]*\$#$\1$#g' $@
}

if [ $# -eq 1 -a $1 = "-a" ]; then
    set -- `find . -depth 3 -name Makefile | sed -E -e 's#^\\./(.*)/Makefile$#\1#'`
fi

for directory
do
	if [ -d "${PORTSDIR}/${directory}" ]; then
		echo "MFC ${PORTSDIR}/${directory} -> ${directory}"
		find ${directory} -type f  ! -regex '.*/\.svn.*' -delete || exit 1
		(cd ${PORTSDIR} && tar cf - --exclude='.svn' ${directory}) | tar xf -
		unsubstitute_keywords `find ${directory} -type f ! -regex '.*/\.svn.*'`
	else
		echo "${directory} is not in the FreeBSD ports (yet)" >&2
	fi
done

